import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;

public class UDPServerPlus1 {
    DatagramSocket socket = null;
    public UDPServerPlus1(int park) throws SocketException {
        this.socket = new DatagramSocket(park);
    }
    private String responseFun(String request){
        return request + "嘎嘎";
    }
    private void start() throws IOException {
        System.out.println("服务器启动");
        while (true){
            DatagramPacket requestPacket = new DatagramPacket(new byte[4096],4096);
            socket.receive(requestPacket);
            String request = new String(requestPacket.getData(),0,requestPacket.getLength());
            String response = responseFun(request);
            DatagramPacket responsePacket = new DatagramPacket(response.getBytes(),response.getBytes().length,
                                                     requestPacket.getSocketAddress());
            socket.send(responsePacket);
            String log = String.format("%s,%s,%s",requestPacket.getSocketAddress(),request,response);
            System.out.println(log);
        }
    }

    public static void main(String[] args) throws IOException {
        UDPServerPlus1 udpServerPlus1 = new UDPServerPlus1(9999);
        udpServerPlus1.start();
    }
}
